
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import com.constant.*;
import com.obscure.delib.*;


interface GUI_MainMenu
{
	public final static int button1 = 1000;
	public final static int button2 = 1001;
	public final static int button3 = 1002;
	public final static int button4 = 1003;
}

public class ScreenStateMM implements ScreenState {

	private SpriteBatch m_spriteBatch;
    private deSprite m_splashDeSprite;
    private deSprite m_button;
    // button
    private GuiElement m_button1;
    private GuiElement m_button2;
    private GuiElement m_button3;
    private GuiElement m_button4;

    private long m_StartTime;
    private boolean m_isStateDone=false;
    
    private boolean isTouchReleased=false;
    private boolean isTouched      =false;
    
    private final BitmapFont m_font;
    
	public int m_NextState=0;
	public ScreenStateMM(Application app) {
		// TODO Auto-generated constructor stub

		m_spriteBatch    = new SpriteBatch();
		m_splashDeSprite = new deSprite();
		m_splashDeSprite.setSptiteBatch(m_spriteBatch);
		m_splashDeSprite.loadData(DATA_SPRITE.SPR_BGMAIN);
		m_StartTime= System.currentTimeMillis();
		
		m_button = new deSprite();
		m_button.setSptiteBatch(m_spriteBatch);
		m_button.loadData(DATA_SPRITE.SPR_BUTTON);
		createGUI();
		m_NextState =GameState.STATE_GP;
		
		m_font = new BitmapFont(Gdx.files.internal("xdata/fonts/font16.fnt"), Gdx.files.internal("xdata/fonts/font16.png"), false);
	}

	private void createGUI()
	{
		// everything is still set manually
		int offsetX =131;
		int offsetY =192;
		m_button1 = new GuiElement(m_button);
		m_button1.setPos(offsetX+40, offsetY+50);
		m_button1.setAnim(SPRITE_BUTTON.ANIM_0);
		m_button1.setGUIWidthHight(195,42);
		m_button1.setGuiID(GUI_MainMenu.button1);

		m_button2 = new GuiElement(m_button);
		m_button2.setPos(offsetX+40, offsetY+120);
		m_button2.setAnim(SPRITE_BUTTON.ANIM_0);
		m_button2.setGUIWidthHight(195,42);
		m_button2.setGuiID(GUI_MainMenu.button2);

		m_button3 = new GuiElement(m_button);
		m_button3.setPos(offsetX+260, offsetY+50);
		m_button3.setAnim(SPRITE_BUTTON.ANIM_0);
		m_button3.setGUIWidthHight(195,42);
		m_button3.setGuiID(GUI_MainMenu.button3);
		
		m_button4 = new GuiElement(m_button);
		m_button4.setPos(offsetX+260, offsetY+120);
		m_button4.setAnim(SPRITE_BUTTON.ANIM_0);
		m_button4.setGUIWidthHight(195,42);
		m_button4.setGuiID(GUI_MainMenu.button4);
			
		GuiElementManager.getInstance().addElement(m_button1);
		GuiElementManager.getInstance().addElement(m_button2);
		GuiElementManager.getInstance().addElement(m_button3);
		GuiElementManager.getInstance().addElement(m_button4);	
	
	}
	@Override
	public void update(Application app) {
		int clickedGUI = GuiElementManager.getInstance().update(Gdx.input);
		if(clickedGUI != -1) {
			executeClickedButton(clickedGUI);
		}
	}

	private void executeClickedButton(int clickedGUI) {
		switch(clickedGUI)
		{
			case GUI_MainMenu.button1	:
				System.out.println("--button1 clicked-- ");
				//m_NextState =GameState.STATE_GP + 2;
				//m_isStateDone=true;
				break;
			case GUI_MainMenu.button2:
				System.out.println("--button2 clicked-- ");
				m_NextState =GameState.STATE_MAP;//.STATE_GP;
				m_isStateDone=true;
				break;
			case GUI_MainMenu.button3:
				System.out.println("--button3 clicked-- ");
				Gdx.app.exit();
				break;
			case GUI_MainMenu.button4:
				System.out.println("--button4 clicked-- ");
				//m_NextState =GameState.STATE_GP + 1;
				//m_isStateDone=true;
				break;
		}		
	}

	@Override
	public void render(Application app) {
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);   //clear screen
        Gdx.gl.glClearColor(255, 255, 255, 255);    //fill screen with white color
		// bg main menu
		m_splashDeSprite.paintFrame(0, 0, 0);
		// all button
		GuiElementManager.getInstance().render();
						
		m_spriteBatch.begin();
		String text = "PLAY";
		TextBounds bounds = m_font.getMultiLineBounds(text);
		m_spriteBatch.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_font.drawMultiLine(m_spriteBatch, text, 0,295 + bounds.height / 2, 520, HAlignment.CENTER);
		
		text = "ACHIEVEMENTS";
		//TextBounds bounds = m_font.getMultiLineBounds(text);
		m_spriteBatch.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_font.drawMultiLine(m_spriteBatch, text, 0, 295 + bounds.height / 2, 970, HAlignment.CENTER);
		
		text = "OPTION";
		//TextBounds bounds = m_font.getMultiLineBounds(text);
		m_spriteBatch.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_font.drawMultiLine(m_spriteBatch, text, 0, 225 + bounds.height / 2, 520, HAlignment.CENTER);
		
		text = "EXIT";
		//TextBounds bounds = m_font.getMultiLineBounds(text);
		m_spriteBatch.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_font.drawMultiLine(m_spriteBatch, text, 0, 225 + bounds.height / 2, 970, HAlignment.CENTER);
		m_spriteBatch.end();
	}

	@Override
	public boolean isDone() {
		return m_isStateDone;
	}

	@Override
	public void dispose() {
		GuiElementManager.getInstance().removeElement(m_button1);
		GuiElementManager.getInstance().removeElement(m_button2);
		GuiElementManager.getInstance().removeElement(m_button3);
		GuiElementManager.getInstance().removeElement(m_button4);
		m_spriteBatch.dispose();
		m_splashDeSprite.dispose();		
	}
	
	public int getNextState() {
		return m_NextState;
	}

}
